package com.shwread.android.ui.widget;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.shwread.android.qysw.bean.que.OptionBean;
import com.shwread.android.qysw10000243.R;

import java.util.List;

import logic.util.ScreenUtil;

/**
 * 题目列表导航
 */
public class QuePopupWindow extends PopupWindow {

    private Context mContext;
    private MyAdapter myAdapter;
    private List<OptionBean> infoList;
    private PopItemListener listener;
    private RecyclerView recyclerView;
    private View view;
    /**
     * 答题数
     */
    private TextView tvQueCursor;
    /**
     * 完成
     */
    private TextView tvQueDone;
    /**
     * 未完成
     */
    private TextView tvQueNotDone;

    public QuePopupWindow(Context context) {
        super(context);
        mContext = context;
        view = LayoutInflater.from(context).inflate(R.layout.pop_que_list_view, null);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        setContentView(view);
        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        setHeight(ScreenUtil.screenHeight);
        this.setFocusable(true);//设置可获取焦点
        update();//刷新状态
        ColorDrawable colorDrawable = new ColorDrawable(0);
        // 点back键和其他地方使其消失,设置了这个才能触发OnDismissListener ，设置其他控件变化等操作
        setBackgroundDrawable(colorDrawable);
        recyclerView = (RecyclerView) view.findViewById(R.id.rlv_list);
        tvQueCursor = (TextView) view.findViewById(R.id.tv_que_cursor);
        tvQueDone = (TextView) view.findViewById(R.id.tv_que_done);
        tvQueNotDone = (TextView) view.findViewById(R.id.tv_que_not_done);
    }

    /**
     * 显示popupWindow
     */
    public void showPopupWindow(int index, List<OptionBean> infoList, PopItemListener listener) {
        if (isShowing()) {
            dismiss();
        } else {
            this.infoList = infoList;
            this.listener = listener;
            int over = 0;
            for (OptionBean optionBean : infoList) {
                if (optionBean.isFinish()) {
                    over ++;
                }
            }
            tvQueCursor.setText(index + 1 + "/" + infoList.size());
            tvQueDone.setText(String.valueOf(over));
            tvQueNotDone.setText(String.valueOf(infoList.size() - over));
            recyclerView.setLayoutManager(new GridLayoutManager(mContext, 5));
            myAdapter = new MyAdapter();
            recyclerView.setAdapter(myAdapter);
            showAtLocation(view, Gravity.BOTTOM, 0, 0);
        }
    }

    private class MyAdapter extends RecyclerView.Adapter<MyHolder> {

        @Override
        public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.item_pop_que_list, parent, false);
            return new MyHolder(view);
        }

        @Override
        public void onBindViewHolder(MyHolder holder, int position) {
            final int index = position;
            holder.tvQuePosition.setText(String.valueOf(position + 1));
            holder.tvQuePosition.setBackgroundResource(infoList.get(position).isFinish() ?
                            R.drawable.btn_que_finish : R.drawable.search_theme_box_background);
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {//关闭pop
                    if (listener != null) {
                        listener.click(index);
                    }
                    dismiss();
                }
            });
        }

        @Override
        public int getItemCount() {
            return infoList.size();
        }
    }

    private class MyHolder extends RecyclerView.ViewHolder {

        private TextView tvQuePosition;//题号

        private MyHolder(View itemView) {
            super(itemView);
            tvQuePosition = (TextView) itemView.findViewById(R.id.tv_que_position);
        }
    }

    public interface PopItemListener {
        void click(int position);
    }
}
